package engine.ui
{
	import com.greensock.TweenLite;
	
	import engine.GameData;
	import engine.logic.AlertLogic;
	import engine.pool.IResoule;
	import engine.pool.ObjectPool;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.filters.BitmapFilterQuality;
	import flash.filters.GlowFilter;
	import flash.geom.Point;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.utils.getTimer;
	import flash.utils.setTimeout;
	
	import newx.StageInfo;
	import newx.com.config.StaticIni;

	/**
	 *特定区域漂浮提示 
	 * @author gaoyq
	 * 
	 */
	public class MsgTipsLogic implements IResoule
	{
		
		private static var  _msgTips:MsgTipsLogic;
		private var _textFormat	:TextFormat;
		
		private var _oldTime		:int = 0;
		
		private var _container 	:DisplayObjectContainer;
		
		private var txt			:TextField;
		
//		private var _txtList		:Array=[];
		
		private var _delay			:int;
		private var _heigthY		:int = 0;
		private var _widthX		:int = 0;
		private var _isRole		:Boolean = false;
		private var _complete		:Function;
		public function MsgTipsLogic( )
		{
			_textFormat	=	new TextFormat();
			_textFormat.size			=	14;
			_textFormat.bold 			=	true;
			_textFormat.color			=	StaticIni.getIniVar("Css_txt_MessageUI", "txtStr");
			 txt  = getMsgTxt();
		}
		
		/***是不是用于显示人物头顶**/
		public function set isRole(value:Boolean):void
		{
			_isRole = value;
		}

		public function set widthX(value:int):void
		{
			_widthX = value;
		}

		public function set heigthY(value:int):void
		{
			_heigthY = value;
		}

		public static function getRes(container:DisplayObjectContainer,x:int = 0,y:int = 0,isRole:Boolean = false):MsgTipsLogic
		{
			var _tipLogic:MsgTipsLogic = ObjectPool.GetPool( MsgTipsLogic ).borrowObject() as MsgTipsLogic;
			_tipLogic.container=container;
			_tipLogic.heigthY = y;
			_tipLogic.widthX = x;
			_tipLogic.isRole = isRole;
			return _tipLogic;
		}
		
		public function set container(value:DisplayObjectContainer):void
		{
			_container = value;
		}

	
		public function reset():void
		{
			container = null;
		}
		
		
		public function showInfo(value:String,isRole:Boolean = false):MsgTipsLogic
		{
//			if(isRole)
//			{
//				this._container = GameData.Role.tipPlace;
//			}
			var msgtipsOldtime:int =  AlertLogic.getInstance().msgTipsTime;
			var msgTipsDelay:int = AlertLogic.getInstance().msgTipsDelay;
			var interval : int = getTimer() - msgtipsOldtime - msgTipsDelay;
			if(  interval < 200 )
			{
				AlertLogic.getInstance().msgTipsDelay = 200 - interval;
			}else
			{
				AlertLogic.getInstance().msgTipsDelay = 0;
			}

			if(!isRole)
			{
				AlertLogic.getInstance().msgTipsDelay = 0;
			}
			
			AlertLogic.getInstance().msgTipsTime  = getTimer();
			var colorStr:String = StaticIni.getIniVar("Css_txt_MessageUI", "txtStr").split("x")[1];
			txt  = getMsgTxt();
			txt.htmlText	="<b><font size='14' color='#" + colorStr + "'>" + value + "</font></b>";
//			txt.setTextFormat(_textFormat);
			
			txt.y = _heigthY;
			txt.alpha = 1;
			
			if(isRole)
			{
				txt.x = - txt.textWidth * .5;//(_container.width- txt.textWidth) * .5 - _widthX;
			}else
			{
				txt.x = (_container.width- txt.textWidth) * .5 - _widthX;
			}
			//把当前需要显示在容器的坐标转化成舞台坐标
			var point:Point = _container.localToGlobal(new Point(txt.x,txt.y));
			//把舞台坐标转化成父容器坐标
			if(!_container.parent)return null;
			var newPoint:Point = _container.parent.globalToLocal(point)
			txt.x = newPoint.x;
			txt.y = newPoint.y;
			if( AlertLogic.getInstance().msgTipsDelay == 0 )
			{
//				_container.addChild(txt);
				_container.parent.addChild(txt);
				TweenLite.to(txt, 3, { alpha:0, y:"-120", onComplete:complete, onCompleteParams:[txt] });
			}else
			{
				setTimeout( function():void
				{
//					_container.addChild(txt);
					_container.parent..addChild(txt);
					txt.htmlText	="<b><font size='18' color='#" + colorStr + "'>" + value + "</font></b>";
					TweenLite.to(txt, 3, { alpha:0, y:"-120", onComplete:complete, onCompleteParams:[txt] });
				}, AlertLogic.getInstance().msgTipsDelay );
			}
			return this;
		}
		public function completeCallBack(complete:Function):MsgTipsLogic
		{
			_complete=complete;
			return this;
		}
		
		private function complete( txt : TextField ) :void
		{
			var parent:DisplayObjectContainer=txt.parent;
			if(parent)
			{
				if(parent is TempContainer)
				{
					ObjectPool.GetPool(TempContainer).returnObject(parent as IResoule);
				}
				parent.removeChild(txt);
			}
			txt.alpha = 1;
			if(_complete!=null)
			{
				_complete();
				_complete=null;
			}
			ObjectPool.returnPool(this);
		}
		
		private function getMsgTxt():TextField
		{
			var txt : TextField = new TextField();
			txt	=	new TextField();
			txt.mouseEnabled	=	false;
			txt.selectable		=	false;
			txt.autoSize	=	TextFieldAutoSize.CENTER;
			txt.alpha 				= 1;
			var glow:GlowFilter		= 	new GlowFilter();
			glow.color 				= 	0x000000;
			glow.alpha 				= 	1;
			glow.blurX 				=	2;
			glow.blurY				= 	2;
			glow.strength			=	4;
			glow.quality			= 	BitmapFilterQuality.HIGH;
			txt.filters		=	[glow];
			txt.mouseEnabled  = false 
			return txt;
		}
		/**
		 *如果不是 DisplayObjectContainer 可以用这个方法临时转
		 * @param target
		 * @return 
		 * 
		 */		
		public static function getTempContainer(target:DisplayObject):DisplayObjectContainer
		{
			var sp:TempContainer=ObjectPool.GetPool( TempContainer ).borrowObject() as TempContainer;
			StageInfo.stage.addChild(sp);
			var point:Point=target.parent.localToGlobal(new Point(target.x,target.y));
			sp.x=point.x+target.width/2;
			sp.y=point.y;
			return sp;
		}
	}
}
import engine.pool.IResoule;

import flash.display.Sprite;

class TempContainer extends Sprite  implements IResoule
{
	public function TempContainer():void
	{
		graphics.drawCircle(0,0,1);
		mouseEnabled=false;
	}
	public function reset():void
	{
		if(parent)
			parent.removeChild(this);
	}
}